import {
  baseURL
} from "./config"
import {
  login
} from "login.js"

class LHRequest {
  constructor(baseURL) {
    this.baseURL = baseURL
    this.options = null
    this.lhRequest_options = {
      isShowLoad: true, //是否显示加载
      isErrorRetryRequest: false, //是否报错重复执行
      requstTitle: "请求", //请求小标题 
    }
    this.resolve = null
    this.reject = null
  }

  /**
   * 发送请求
   */
  async request() {
    console.log(this.options)
    console.log(this.lhRequest_options)
    const {
      isShowLoad,
      isErrorRetryRequest,
      requstTitle
    } = this.lhRequest_options

    if (isShowLoad) {
      await wx.p.showLoading({
        title: '加载中',
        mask: true
      })
    }
    try {
      let res = await wx.p.request({
        ...this.options,
        url: this.baseURL + this.options.url
      })
      console.log(res.data)
      if (res.data.message == 'success') {
        this.resolve && await this.resolve(res.data.data)
        isShowLoad && wx.p.hideLoading()
      } else {
        const {
          errcode
        } = res.data

        if (errcode == -1) {
          await wx.p.showModal({
            title: `登录失效`,
            content: '为您重新登录',
            showCancel: false
          })
          //重新登录
          this.options.data.loginkey =  await login()
          this.request()
        } else if (isErrorRetryRequest) {
          throw new Error("inner-error")
        } else if (this.reject) {
          // 自定义失败操作
          await this.reject(res)
        } else { // 显示报错内容可继续操作 
          await wx.p.showModal({
            title: res.data.message && `${requstTitle}失败` || '网络波动',
            content: res.data.message || '请您刷新小程序重试',
            showCancel: false
          })
          isShowLoad && wx.p.hideLoading()
        }
      }
    } catch (err) {
      console.log(err)
      try {
        // 报错后无限刷新重试，不能继续操作
        await wx.p.showModal({
          title: '网络连接问题',
          content: `${requstTitle}失败，可能是网络连接问题。请检查网络后重试。`,
          showCancel: false
        })
        setTimeout(()=>{
          this.request()
        },3000)
      } catch {       
        setTimeout(()=>{
          this.request()
        },3000)
      }
    }
  }

  /**
   * 发送get请求
   * @param {
   *   lhRequest_options: {
   *     isShowLoad: true,//是否显示加载
   *     isErrorRetryRequest: false,//是否报错重复执行
   *     requstTitle: "请求",//请求小标题 
   *   } 
   * } options 
   * @param {*} resolve 
   * @param {*} reject 
   */
  async get(options, resolve, reject) {
    this.lhRequest_options = {
      ...this.lhRequest_options,
      ...options.lhRequest_options
    }
    delete options.lhRequest_options
    this.options = options
    this.options.method = "get"
    this.resolve = resolve
    this.reject = reject
    return await this.request()
  }
  /**
   * 发送post请求
   * @param {
   *   lhRequest_options: {
   *     isShowLoad: true,//是否显示加载
   *     isErrorRetryRequest: false,//是否报错重复执行
   *     requstTitle: "请求",//请求小标题 
   *   } 
   * } options 
   * @param {*} resolve 
   * @param {*} reject 
   */
  async post(options, resolve, reject) {
    this.lhRequest_options = {
      ...this.lhRequest_options,
      ...options.lhRequest_options
    }
    delete options.lhRequest_options
    this.options = options
    this.options.method = "post"
    this.resolve = resolve
    this.reject = reject
    return await this.request()
  }
}

export const lhRequest = new LHRequest(baseURL)